<?php
namespace Admin\Controller;

use Common\Util\Page;
use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;

class UserController extends AdminbaseController {
    public function index(){
    	$UserModel = D('User');
    	
    	$openId = I ('get.openid');
    	$mobile = I ('get.mobile');
    	$userId = I ('get.userId');
    	 
    	$where = array();
    	if ($openId) {
    		$where ['open_id'] = $openId;
    	}
    	 
    	if ($mobile) {
    		$where ['mobile'] = array('like', '%'.$mobile.'%');
    	}
    	 
    	if ($userId) {
    		$where ['id'] = $userId;
    	}
    	
   		$count = $UserModel->where($where)->count();
   		$page = $this->page($count, 20);
   		
    	$list = $UserModel->where($where)->order('created desc')
    		->limit($page->firstRow.','.$page->listRows)->select();
    	
    	$this->assign('list', $list);
    	$this->assign('Page', $page->show('Admin'));
    	$this->display();
    }
    
    public function searchUser() {
    	$UserModel = D('User');
    	
    	$key = I('get.query');
    	
    	$condition["mobile"] = array('like', '%'.$key.'%');
    	$users = $UserModel->field(array('id', 'mobile' => 'value', 'username', 'headerimg'))->where($condition)->select();
    	
    	$data = array(
    		'query' => $key,
    		'suggestions' =>  $users
    	);
    	$this->ajaxReturn($data);
    }
    
    public function clearBind() {
    	$id = I ('get.id');
    	
    	$UserModel = D('User');
    	$result = $UserModel->clearBind($id);
    	if ($result) {
    		$this->success('清除绑定成功');
    	} else {
    		$this->success('清除绑定失败');
    	}
    }
}